package com.csjy.migration.hnnd;

import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.business.AbstractBusiness;
import com.csjy.migration.hnnd.business.NdProjectInfo;
import com.csjy.migration.hnnd.business.SourceBusinessData;
import com.csjy.migration.hnnd.workflow.WorkflowMigration;

import java.io.FileWriter;
import java.lang.reflect.Constructor;
import java.util.*;
import java.util.stream.Collectors;

public class ProjectMain {
  private DbUtil sourceDbUtil;
  private DbUtil targetDbUtil;
  private String projectId;
  private ProgramArgument programArgument;

  public ProjectMain(DbUtil sourceDbUtil, DbUtil targetDbUtil, ProgramArgument programArgument, String projectId) {
    this.sourceDbUtil = sourceDbUtil;
    this.targetDbUtil = targetDbUtil;
    this.projectId = projectId;
    this.programArgument = programArgument;
  }

  public void start() throws Exception {
    targetDbUtil.updateByParams("DELETE FROM MIGRATION_ID_MAP WHERE SOURCE_ID = '"+this.projectId+"-JW"+"'", new ArrayList());

    // 缓存当前项目老数据库的数据
    SourceBusinessData sourceBusinessData = new SourceBusinessData(this.sourceDbUtil, this.projectId);

    // 首先迁移项目
    List<Object> projectList = this.targetDbUtil.selectSingleColumn("SELECT id FROM ND_PROJECT_INFO WHERE id = ?" ,Arrays.asList(this.projectId));
    if(projectList.size() == 0) {
      new NdProjectInfo(sourceDbUtil,targetDbUtil,sourceBusinessData,programArgument, projectId).start();
    }

    // 迁移当前项目基础表数据
    Set<Class<?>> set = Tool.getAnnotationClasses("com.csjy.migration.hnnd.business", BusinessDataConverter.class);

    List<Class> tableClasses = set.stream().sorted(Comparator.comparing(c->{
      BusinessDataConverter businessDataConverter = c.getAnnotation(BusinessDataConverter.class);
      return businessDataConverter.seq();
    })).collect(Collectors.toList());

    for (Class<?> c : tableClasses) {
      BusinessDataConverter businessDataConverter = c.getAnnotation(BusinessDataConverter.class);

      if(!businessDataConverter.auto()){
        continue;
      }

      if(!programArgument.getTableNames().equals("")){
        String[] tableNameArr = programArgument.getTableNames().split(",");
        String firstTableName = null;
        for(int i = 0;i<tableNameArr.length;i++) {
          if((tableNameArr[i].toLowerCase().replace("_","")).equals(c.getSimpleName().toLowerCase())){
            firstTableName = tableNameArr[i];
            break;
          }
        }
        if(firstTableName == null){
          continue;
        }
      }
      else if(!programArgument.getExcludeTableNames().equals("")) {
        String[] tableNameArr = programArgument.getExcludeTableNames().split(",");
        String firstTableName = null;
        for(int i = 0;i<tableNameArr.length;i++) {
          if((tableNameArr[i].toLowerCase().replace("_",""))
              .equals(c.getSimpleName().toLowerCase())){
            firstTableName = tableNameArr[i];
            break;
          }
        }
        if(firstTableName != null){
          continue;
        }
      }

      Constructor<AbstractBusiness> ctor = (Constructor<AbstractBusiness>)
          c.getConstructor(DbUtil.class,DbUtil.class,SourceBusinessData.class, ProgramArgument.class, String.class);
      ctor.newInstance(sourceDbUtil,targetDbUtil,sourceBusinessData,programArgument, projectId).start();
    }
    
    targetDbUtil.insertMigrationIdMap(this.projectId+"-JW");

  }
}
